package com.wardrobe.soa.utils;

import org.apache.commons.lang.StringUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class NumberUtil {

    public static double getAppVersionAsDouble(String v, double defultValue) {
        try {
            String tempVer = v.replaceFirst("[^0-9\\.].*", "");
            String[] tempArr = StringUtils.split(tempVer, ".");

            tempVer = tempVer.replace(".", "");

            StringBuilder subd = new StringBuilder(tempVer);
            tempVer = subd.insert(tempArr[0].length(), ".").toString();
            return parseDouble(tempVer, defultValue);
        } catch (Exception e) {
        }
        return defultValue;
    }

    public static Double parseDouble(Object val, Double defultValue) {
        try {
            return Double.parseDouble(val.toString().trim());
        } catch (Exception e) {
            return defultValue;
        }
    }

    public static float parseFloat(Object val, float defultValue) {
        try {
            return Float.parseFloat(val.toString().trim());
        } catch (Exception e) {
            return defultValue;
        }
    }

    public static int parseInt(Object val, int defultValue) {
        try {
            return Integer.parseInt(val.toString().trim());
        } catch (Exception e) {
            return defultValue;
        }
    }

    public static long parseLong(Object val, long defultValue) {
        try {
            return Long.parseLong(val.toString().trim());
        } catch (Exception e) {
            return defultValue;
        }
    }

    public static List<Integer> parseToIntegerList(String[] prams) {
        LinkedList<Integer> ret = new LinkedList<Integer>();
        if (prams == null) {
            return ret;
        }
        for (String pram : prams) {
            try {

                if(pram.trim()!=""){
                    ret.add(Integer.parseInt(pram.trim()));
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ret;
    }


    public static String random(){
        int max=5;
        int min=1;
        Random random = new Random();
        return String.valueOf(random.nextInt(max)%(max-min+1) + min);
    }
}
